import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, GAME_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { ResManager1 } from "../manager/ResManager1";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadUI extends cc.Component {
    @property(cc.ProgressBar)
    loadingBar: cc.ProgressBar = null;
    @property(cc.Label)
    percentLbl: cc.Label = null;
    @property(cc.Label)
    descLbl: cc.Label = null;

    start() {
        this.loadRes(100);
    }

    /**
     * @param step 加载步数
     */
    async loadRes(step: number) {
        let updateProgress = (pass: number, total: number) => {
            let num = Math.round(pass / total * 100);
            this.loadingBar.progress = num / 100;
            this.percentLbl.string = num + '%';
        };
        let progress = 0;
        switch (step) {
            case 100:
                this.descLbl.string = "加载资源中";
                this.percentLbl.string = "0%";
                this.loadingBar.progress = 0;
                await ResManager1.ins.loadHomeAssetsBundle();
                await ResManager1.ins.loadLevelSprBundle();
                cc.log("HomeAssets 加载完毕")
                break;
            case 99:
                progress = Tools.random(1, 50)
                this.percentLbl.string = progress + "%";
                this.loadingBar.progress = progress / 100;
                await ResManager1.ins.loadGameAssetsBundle();
                cc.log("GameAssets 加载完毕")
                break;
            case 98:
                progress = Tools.random(51, 80)
                this.percentLbl.string = progress + "%";
                this.loadingBar.progress = progress / 100;
                await ResManager1.ins.loadRes('prefab/node/lvItem', cc.Prefab, BUNDLE_TYPE_ENUM.HOME);
                break;
            default:
                this.descLbl.string = "加载完成";
                this.percentLbl.string = "100%";
                this.loadingBar.progress = 1;
                this.enterGame();
                return;
                break;
        }
        this.loadRes(--step);
    }

    enterGame() {
        UIManager.ins.close();
        if (AdManager.releaseType === RELEASE_TYPE_ENUM.oppo_mini || AdManager.releaseType === RELEASE_TYPE_ENUM.vivo_mini) {
            UIManager.ins.showUI("startUI", BUNDLE_TYPE_ENUM.HOME);
            return;
        }
        switch (AdManager.gameType) {
            case GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN:
            case GAME_TYPE_ENUM.WO_YAO_BIAN_TU_LE:
            case GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE:
            case GAME_TYPE_ENUM.WAN_DAN_ZHANG_NAO_ZI_LE:
            case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
            case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
            case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
                UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
                break;
            default:
                UIManager.ins.showUI("startUI", BUNDLE_TYPE_ENUM.HOME);
                break;
        }
    }
}